/*
 * PathOrientedVertex.java
 *
 * Created on September 9, 2007, 1:26 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package dnbW.queryW.visualW;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

/**
 *
 * @author Colin
 */
public class PathOrientedVertex extends PredicateVertex {
    
    protected List<DefaultMutableTreeNode> _path;
    private List<DefaultMutableTreeNode> _fullPath;
    private int fromIndex = -1;
    private int toIndex = -1;
    
    /** Creates a new instance of PathOrientedVertex */
    public PathOrientedVertex(DefaultMutableTreeNode node) {
        _path = this.constructPath(node);
        _fullPath = Collections.unmodifiableList(_path);
    }
    
    protected List<DefaultMutableTreeNode> constructPath(DefaultMutableTreeNode node) {
        List<DefaultMutableTreeNode> path = new ArrayList<DefaultMutableTreeNode>();
        
        for (TreeNode n : node.getPath()) {
            DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) n;
            if (dmtn.toString().equals("<root>")) {
                continue;
            }
            path.add(new DefaultMutableTreeNode(dmtn.getUserObject()));
        }
        
        for (int i = 1; i < path.size(); i++) {
            path.get(i - 1).add(path.get(i));
        }
        
        return path;
    }
    
    public String getXPath() {
        if (getDMTNPath().size() == 1) {
            return "//" + getDMTNPath().get(0).toString();
        }
        String path = "";
        for (int i = getDMTNPath().size() - 1; i >= 0; i--) {
            DefaultMutableTreeNode step = getDMTNPath().get(i);
            path = step.toString() + path;
            
            if (step.isRoot()) {
                path = "/" + path;
            } else {
                if (getDMTNPath().contains(step.getParent())) {
                    path = "/" + path;
                } else {
                    path = "//" + path;
                }
            }
        }
        
        return path;
    }
    
    public List<DefaultMutableTreeNode> getDMTNPath() {
        return _path;
    }
    
    public String toString() {
        return _path.get(_path.size() - 1).toString();
    }
    
    public List<DefaultMutableTreeNode> getFullPath() {
        return _fullPath;
    }

    public int getFromIndex() {
        return fromIndex;
    }

    public void setFromIndex(int fromIndex) {
        this.fromIndex = fromIndex;
    }

    public int getToIndex() {
        return toIndex;
    }

    public void setToIndex(int toIndex) {
        this.toIndex = toIndex;
    }
}